package com.jy.rhin.facade.controller.notification;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jy.framework.base.entity.Result;
import com.jy.rhin.application.basic.event.PublishMessageEvent;
import com.jy.rhin.facade.controller.notification.dto.MessageDto;
import com.jy.rhin.facade.schedule.MessageSchedule;
import com.jy.rhin.sdk.dto.notification.resp.PublishMessageDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author WeiQ
 * @date 2022/7/6 10:11
 */
@RestController
@RequestMapping("/notify")
@Api(hidden = true)
@Slf4j
public class TestController {


    @Resource
    ApplicationEventPublisher eventPublisher;
    @Resource
    MessageSchedule messageSchedule;

    @PostMapping("/testNotify")
    @ApiOperation(value = "testNotify", hidden = true)
    public Result<Boolean> testNotify(@RequestBody MessageDto messageDto) {
        String message = messageDto.getMessage();
        List<PublishMessageDto> publishMessageList = JSON.parseArray(message, PublishMessageDto.class);
        eventPublisher.publishEvent(new PublishMessageEvent(publishMessageList));
        return Result.success(true);
    }

    @PostMapping("/message")
    @ApiOperation(value = "message", hidden = true)
    public void message(@RequestBody JSONObject jsonObject) {
        log.info("收到http通知{}", jsonObject);
    }

    @PostMapping("/triggerSchedule")
    @ApiOperation(value = "triggerSchedule", hidden = true)
    public Result<Boolean> triggerSchedule() {
        messageSchedule.sendFailedMessageCompensation();
        return Result.success(true);
    }


}
